package com.westa.smallshop.register;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.westa.smallshop.data.source.AccountDataSource;
import com.westa.smallshop.data.source.remote.BaseDataSource;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by cy123 on 2016/9/12.
 */
public class RegisterPresenter implements RegisterContract.Presenter {

    @NonNull
    private final RegisterContract.View mRegisterFirstStepView;

    @NonNull
    private final AccountDataSource mAccountDataSource;

    public RegisterPresenter(@NonNull RegisterContract.View registerFirstStepView,
                             @NonNull AccountDataSource accountDataSource) {
        mRegisterFirstStepView = checkNotNull(registerFirstStepView);
        mAccountDataSource = checkNotNull(accountDataSource);

        mRegisterFirstStepView.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void destroy() {
        mAccountDataSource.cancelLoadRequest();
    }

    @Override
    public boolean doCheckInfo(String account, String password, String passwordRepeat) {
        if (TextUtils.isEmpty(account)) {
            mRegisterFirstStepView.showPhoneNumberEmptyError();
            return false;
        }

        if (TextUtils.isEmpty(password)) {

            mRegisterFirstStepView.showPasswordEmptyError();
            return false;
        }
        if (TextUtils.isEmpty(passwordRepeat)) {
            mRegisterFirstStepView.showPasswordRepeatEmptyError();
            return false;
        }

        if (password.length() < 6) {
            mRegisterFirstStepView.showPasswordTooShort();
            return false;
        }
        if (!TextUtils.equals(password, passwordRepeat)) {
            mRegisterFirstStepView.showPasswordNotSameError();
            return false;
        }

        return true;

    }

    @Override
    public void doCreate(String phoneNumber, String password) {
        mRegisterFirstStepView.showRegisterDialogProgress();
        mAccountDataSource.register(phoneNumber, password, new BaseDataSource.LoadCallBack<JSONObject>() {
            @Override
            public void onLoadDataSuccessful(JSONObject data) {
                if (mRegisterFirstStepView.isActive()) {
                    if (TextUtils.equals(data.getString("result"), "0")) {
                        mRegisterFirstStepView.dismissDialogProgress();
                        mRegisterFirstStepView.showCreateSuccessful();
                    } else {
                        mRegisterFirstStepView.dismissDialogProgress();
                        mRegisterFirstStepView.showCreateFailed("注册失败");
                    }
                }
            }

            @Override
            public void onLoadDataFailed() {
                mRegisterFirstStepView.dismissDialogProgress();
                mRegisterFirstStepView.showCreateFailed("注册失败");
            }
        });
    }


    @Override
    public void cancelRegister() {

        mAccountDataSource.cancelLoadRequest();
    }
}
